<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="../inc/conn.asp"-->
<!--#include file="../inc/md5.asp"-->
<!--#include file="../common/admin.asp"-->
<%
'列表
id=request.QueryString("id")
search=request.QueryString("search")
if search="搜索" then
	key=request.QueryString("key")
	mark=request.QueryString("mark")
	StartTime=request.QueryString("StartTime")
    EndTime=request.QueryString("EndTime")
	if key<>"" then
		sqlsearch=" and (title like '%"&key&"%')"
	end if
	if startTime<>"" and EndTime<>"" then sqlsearch=sqlsearch+" and Convert(char(10),addtime,20)>=Convert(char(10),'"&startTime&"',20) and Convert(char(10),addtime,20)<=Convert(char(10),'"&EndTime&"',20) "
end if


'分页-------------------------------------------------------
set Rs=server.CreateObject("ADODB.RecordSet")
sql="select count(sortid) from (Select ROW_NUMBER() OVER (order by id desc ) as sortid,* From PurchaseHistory) as TempTable where 1=1 "&sqlsearch&""
Pager_Pagesize=10 '每页记录数
Rs.open sql,conn,1,1
Pager_TotalRecord=Rs(0) '总记录数
Rs.close
Pager_PageCount=int((Pager_TotalRecord/Pager_Pagesize)+0.99999) '计算总页数
CurrentPage=Request("page") '当前页
'如果不为数字则直接设置页码为1
if isnumeric(CurrentPage)=False Or CurrentPage="" then
	CurrentPage=1
'如果当前页数大于总页数，则直接设置页码为1
elseif  Clng(CurrentPage)>Pager_PageCount then
	CurrentPage=1
end if
Fieldsname=" [id],[title],[sid],[pid],[url],mark,[price],[qty],[TransferCost],[TransportCost],[totalcost],[paytype],[addtime],sortid"
orderBy=" Order by sortid asc"
'拆分SQL语句，必须是select开头，有唯一标识的列
if instr("select",sql)<>-1 then
	if CurrentPage = 1 then
		sql = replace(sql,"select count(sortid)"," select  top " & Pager_Pagesize  & Fieldsname)
	else  
		sql = replace(sql,"select count(sortid)"," select  top " & Pager_Pagesize   & Fieldsname) & " and sortid >(select max(sortid) from (" & replace(sql,"select count(sortid)"," select  top " & Pager_Pagesize * (CurrentPage - 1)  & Fieldsname) & orderBy & ") AA)"
	end if
end if
sql = sql & orderBy
Rs.open sql,conn,1,1
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>main</title>
<link href="../css/style.css" rel="stylesheet" type="text/css"/>
<script language="javascript" type="text/javascript" src="../js/bd.js"></script>
<script language="javascript" type="text/javascript" src="../My97DatePicker/WdatePicker.js"></script>



<script >
function add(){

document.getElementById("totalcost").value=((parseInt(document.getElementById("price").value)*parseInt(document.getElementById("qty").value))+parseInt(document.getElementById("TransferCost").value)+parseInt(document.getElementById("TransportCost").value))-parseInt(document.getElementById("Point").value);
}
</script>
</head>

<body onload="add()">
<div class="main">
  <div class="main-title">购买记录管理: <!--<a href="?id=<%=id%>&a=y">添加</a>--> &nbsp;&nbsp;
    <%
  if parent_cid<>"" then response.Write "<a href='?cid="&parent_cid&"'>上一级</a>"
  if fid<>"" then response.Write "<a href='?id="&fid&"'>上一级</a>"
  %></div>
  <div class="main-content">
  
  
  <%if request.QueryString("a")="y" then%>
  <table width="100%" border="0" cellspacing="1" cellpadding="3" class="tab">
  <form name="theForm" id="demo" method="post" onSubmit="return Validator.Validate(this,3)">
  <tr>
    <td bgcolor="#FFFFFF">商品名</td>
    <td colspan="6" bgcolor="#FFFFFF"><input name="title" type="text" class="input" id="title" value="<%=request.QueryString("title")%>" maxlength="50" datatype="LimitB" min="1" max="50" msg="分类名称[1,50]字符以内" /></td>
  </tr>
  <tr>
    <td width="10%" bgcolor="#FFFFFF">购买来源</td>
    <td width="90%" colspan="6" bgcolor="#FFFFFF">
    
    <select name="sid">
        <option disabled>实体店</option>
                    <%		
					set rsnewx=server.CreateObject("adodb.recordset")
					sql="select sid,title from shop where typeid=0"
					rsnewx.open sql,conn,1,3
					if not rsnewx.eof then
					do while not rsnewx.eof 
					%>
					<option value="<%=rsnewx("sid")%>"<%if trim(rsnewx("sid"))=request.QueryString("source") then response.Write " selected"%>>　<%=rsnewx("title")%></option>
					<%
					rsnewx.movenext
					loop
					rsnewx.close
					set rsnewx=nothing
					end if		 
					%>
         <option disabled>购物网</option> 
                    <%		
					set rsnewx=server.CreateObject("adodb.recordset")
					sql="select sid,title from shop where typeid=1 "
					rsnewx.open sql,conn,1,3
					if not rsnewx.eof then
					do while not rsnewx.eof 
					%>
					<option value="<%=rsnewx("sid")%>"<%if trim(rsnewx("sid"))=request.QueryString("source") then response.Write " selected"%>>　<%=rsnewx("title")%></option>
					<%
					rsnewx.movenext
					loop
					rsnewx.close
					set rsnewx=nothing
					end if		 
					%>
        </select>
    
    </td>
  </tr>
  <tr style="display:none">
    <td bgcolor="#FFFFFF">来源地址</td>
    <td colspan="6" bgcolor="#FFFFFF"><input name="url" type="text" class="input" id="url" value="#" datatype="LimitB" min="1" max="500" msg="关键字[1,500]字符以内" /></td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF">单价</td>
    <td colspan="6" bgcolor="#FFFFFF"><input name="price" type="text" class="input" id="price" value="<%=request.QueryString("price")%>" onpropertychange="add()" oninput="add()" /></td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF">数量</td>
    <td colspan="6" bgcolor="#FFFFFF"><input name="qty" type="text" class="input" id="qty" value="<%=request.QueryString("qty")%>" onpropertychange="add()" oninput="add()" /></td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF">银行转账费</td>
    <td colspan="6" bgcolor="#FFFFFF"><input name="TransferCost" type="text" class="input" id="TransferCost" value="0" onpropertychange="add()" oninput="add()" /></td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF">日本国内运费</td>
    <td colspan="6" bgcolor="#FFFFFF"><input name="TransportCost" type="text" class="input" id="TransportCost" value="<%=request.QueryString("transCost")%>" onpropertychange="add()" oninput="add()" /></td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF">使用积分</td>
    <td colspan="6" bgcolor="#FFFFFF"><input name="Point" type="text" class="input" id="Point" value="0" onpropertychange="add()" oninput="add()" /></td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF">总费用</td>
    <td colspan="6" bgcolor="#FFFFFF"><input name="totalcost" type="text" class="input" id="totalcost" datatype="LimitB" min="1" max="500" msg="关键字[1,500]字符以内" /></td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF">支付方式</td>
    <td colspan="6" bgcolor="#FFFFFF"><select name="paytype" id="paytype">
      <option value="1">现金</option>
      <option value="2">UFJ银行转账</option>
      <option value="3">Abc银行转账</option>
      <option value="4">Abc信用卡支付</option>
      <option value="5">CDE信用卡支付</option>
      <option value="6">EFD信用卡支付</option>
      </select></td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF">购买时间</td>
    <td colspan="6" bgcolor="#FFFFFF"><input name="addtime" type="text" class="input Wdate" id="addtime" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" value="<%=now()%>"  /></td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF">&nbsp;</td>
    <td colspan="6" bgcolor="#FFFFFF">
      <input name="pid" type="hidden" value="<%=request.QueryString("pid")%>" />
      <input name="product" type="hidden" value="<%=request.QueryString("product")%>" />
      <input name="oid" type="hidden" value="<%=request.QueryString("oid")%>" />
      <input name="orderqty" type="hidden" value="<%=request.QueryString("qty")%>" />
      <input name="buyqty" type="hidden" value="<%=request.QueryString("buyqty")%>" />
      <input type="submit" name="button" id="button" value="添加" onClick="Validator.Validate(document.getElementById('demo'),3)" />
      <a href="javascript:;" onClick="javascript:history.back(-1);">返回</a>
      
      </td>
  </tr>
    </form>
   </table>
   <%end if%>

  
  
  
  
  
  <table width="100%" border="0" cellspacing="1" cellpadding="3" class="tab">
  <tr>
    <td colspan="6" align="center" bgcolor="#FFFFFF">
    <form name="form" method="get" action="">
    商品名<input name="key" type="text" id="key" value="<%=key%>" maxlength="50" />
    
    购买日期<input name="StartTime" type="text" class="Wdate" id="startTime" onFocus="var EndTime=$dp.$('EndTime');WdatePicker({doubleCalendar:true,onpicked:function(){EndTime.focus();},maxDate:'#F{$dp.$D(\'EndTime\')}'})" value="<%=startTime%>" size="20" readonly="readonly" /> -
<input name="EndTime" type="text" class="Wdate" id="EndTime" onFocus="WdatePicker({doubleCalendar:true,minDate:'#F{$dp.$D(\'startTime\')}'})" value="<%=EndTime%>" size="20" readonly="readonly" />
    
    <input type="submit" name="search" value="搜索" />
    </form>
    </td>
    </tr>
    </table>
  <table width="100%" border="0" cellspacing="1" cellpadding="3" class="tab">
  <form action="" method="post">
  <tr>
    <td align="center" bgcolor="#FFFFFF"><strong>选择</strong></td>
    <td align="center" bgcolor="#FFFFFF">商品名</td>
    <td align="center" bgcolor="#FFFFFF"><strong>Sid</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>来源地址</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>单价</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>数量</strong></td>
    <td align="center" bgcolor="#FFFFFF">银行转账费</td>
    <td align="center" bgcolor="#FFFFFF"><strong>日本国内运费</strong></td>
    <td align="center" bgcolor="#FFFFFF">总费用</td>
    <td align="center" bgcolor="#FFFFFF">购买时间</td>
    <td align="center" bgcolor="#FFFFFF">状态</td>
    <td align="center" bgcolor="#FFFFFF">支付方式</td>
    </tr>
    <%do while not Rs.eof%>
  <tr>
    <td align="center"><input type="checkbox" name="checkbox" id="checkbox" value="<%=rs("Sid")%>" /></td>
    <td align="center"><%=rs("title")%></td>
    <td align="center"><%=rs("Sid")%></td>
    <td align="center"><%=rs("url")%></td>
    <td><%=rs("price")%></td>
    <td align="center"><%=rs("qty")%></td>
    <td align="center"><%=rs("TransferCost")%></td>
    <td align="center"><%=rs("TransportCost")%></td>
    <td align="center"><%=rs("totalcost")%></td>
    <td align="center"><%=rs("addtime")%></td>
    <td align="center">
          <%
		  select case rs("mark")
		  case 0: response.write("取消")
		  case 1: response.write("正常")
	      end select
		  %>
    </td>
    <td align="center">
      <%
		  select case rs("paytype")
		  case 1: response.write("现金")
		  case 2: response.write("UFJ银行转账")
		  case 3: response.write("Abc银行转账")
		  case 4: response.write("Abc信用卡支付")
		  case 5: response.write("CDE信用卡支付")
		  case 6: response.write("EFD信用卡支付")
	      end select
		  %>
    </td>
    </tr>
    <%
	Rs.movenext
	loop
	Rs.close
	Set Rs=nothing
	%>
    <tr>
      <td colspan="12" align="center" bgcolor="#FFFFFF" class="page">
      </td>
    </tr>
    </form>
    <tr>
    <td colspan="12" align="center" bgcolor="#FFFFFF" class="page">　
      <%
	siteurl=request.servervariables("document_name")
	%>
    <form action="<%=siteurl%>" method="get">
   
        <%
		pageurl=siteurl&"?"&"key="&key&"&StartTime="&StartTime&"&EndTime="&EndTime&"&mark="&mark&"&cid="&cid&"&id="&id&"&search="&search&"&"
		if CurrentPage<=1 then
		pageview="<a> < </a>"
		else
		pageview="<a href='"&pageurl&"page="&CurrentPage-1&"'> < </a>"
		end if
		if CurrentPage>=7 then
		pageview=pageview+"<a href='"&pageurl&"page=1'>1</a><a>...</a>"
		end if
		for tp = -4 to 4
		if Pager_PageCount>=tp+CurrentPage and tp+CurrentPage=>1  then 
		if trim(tp+CurrentPage)=CurrentPage or tp=0 then
		pageview=pageview+"<a class='cur_page'>"&tp+CurrentPage&"</a>"
		else
		pageview=pageview+"<a"&pagecss&" href='"&pageurl&"page="&tp+CurrentPage&"'>"&tp+CurrentPage&"</a>"
		end if
		end if
		next
		if Pager_PageCount>tp+CurrentPage then pageview=pageview+"<a>...</a>"
		if Pager_PageCount<CurrentPage+1 then
		pageview=pageview+"<a> > </a>"
		else
		pageview=pageview+"<a href='"&pageurl&"page="&CurrentPage+1&"'> > </a>"
		end if
		response.Write pageview
		%>
		&nbsp;
		<input name="page" type="text" value="<%=CurrentPage%>" size="1" maxlength="10" />
        <input type="submit" name="button" value="跳转" />
        <input type="hidden" name="cid" value="<%=cid%>" />
        <input type="hidden" name="id" value="<%=id%>" />
        <input type="hidden" name="key" value="<%=key%>" />
        <input type="hidden" name="StartTime" value="<%=StartTime%>" />
        <input type="hidden" name="EndTime" value="<%=EndTime%>" />
        <input type="hidden" name="mark" value="<%=mark%>" />
        <input type="hidden" name="search" value="搜索" />
        </form>
      </td>
    </tr>
  </table>
</div>
</div>
</body>
</html>
<%
if request.Form("button")="添加" then

	title=request.Form("title")
	sid=request.Form("sid")
	url=request.Form("url")
	price=request.Form("price")
	qty=request.Form("qty")
	TransferCost=request.Form("TransferCost")
	TransportCost=request.Form("TransportCost")
	totalcost=request.Form("totalcost")
	paytype=request.Form("paytype")
	addtime=request.Form("addtime")
	pid=request.Form("pid")
	orderqty=request.Form("orderqty")
	orderdetailID=request.Form("oid")
	buyqty=request.Form("buyqty")
	Point=request.Form("Point")
	product=request.Form("product")
	
	if title="" or sid="" or url="" or price="" or qty="" or TransferCost="" or TransportCost="" or totalcost="" or paytype="" or addtime="" or pid="" then
	response.write"<script>alert('全部不能为空');</script>"
	response.write"<script>window.history.back(-1);</script>"
	response.end
	end if

	
	 '判断product=buy
	if product="buy" then
	    '生成订单
	    '产生订单号（内部和外部）及定单产生日期及其他信息
		BranchID="0022"
		CoNo="000040"
		'交易日期，格式：YYYYMMDD
		yy=right(year(date),2)
		mm=right("00"&month(date),2)
		dd=right("00"&day(date),2)
		riqi=yy & mm & dd
		'生成订单号所有所需元素,格式为：小时，分钟，秒
		xiaoshi=right("00"&hour(time),2)
		fenzhong=right("00"&minute(time),2)
		miao=right("00"&second(time),2)
		'产生外部和内部定单号
		BillNo=xiaoshi & fenzhong & miao
		inBillNo=yy & mm & dd & "" & xiaoshi & fenzhong & miao
		'订单号生成完成
		'订单列表信息写入
		Set rsadd=server.createobject("adodb.recordset")
		rsadd.Open "select * from OrderList" ,conn,2,3
		rsadd.AddNew 
		rsadd("order")=inBillNo '订单号
		rsadd("uid")="-100" '会员id
		rsadd("Money")=totalcost '订单总金额
		rsadd("Promoney")=formatNumber((cdbl(price)*cdbl(qty))+cdbl(TransportCost),2,-1) '商品总金额
		rsadd("ServiceCost")=0 '服务费
		rsadd("Canlackpro")=1 '是否可以缺货
		rsadd("Rate")=web_rate '汇率
		rsadd("usertype")="-100" '会员级别
		rsadd("flag")=1 '默认
		rsadd("adminname")=request.Cookies("admin")("name")
		rsadd.Update
		rsadd.close
		set rsadd=nothing
		'订单列表信息写入完成
		'订单产品写入
		set rsproduct=conn.execute("select * from product where pid="&pid&"") '产品
		  Set rsdetail=server.createobject("adodb.recordset")
		  rsdetail.Open "select * from OrderDetail" ,conn,1,3
		  rsdetail.AddNew 
		  rsdetail("order")=inBillNo  '订单编号
		  rsdetail("uid")="-100"  '会员ID
		  rsdetail("Sid")=sid '来源ID
		  rsdetail("pid")=pid '商品ID
		  rsdetail("title")=title '产品名称
		  rsdetail("url")="#" '产品图片
		  rsdetail("weight")=rsproduct("weight") '产品链接
		  rsdetail("price")=price '产品价格
		  rsdetail("transCost")=TransportCost '日本国内运费
		  rsdetail("Qty")=qty '数量
		  rsdetail("BuyQty")=qty '已购买数量
		  rsdetail("Content")="备注"
		  rsdetail("Type")=0 '类型
		  rsdetail("mark")=4 '默认
		  rsdetail("usertype")="-100" '会员级别
		  rsdetail.Update
		  temp = rsdetail.bookmark 
		  rsdetail.bookmark = temp 
		  orderdetailID=rsdetail("id") 
		  rsdetail.close
		  set rsdetail=nothing
		set rsproduct=nothing
	else

		'更新
		toqty=cdbl(qty)-cdbl(orderqty)
		if cdbl(toqty)>=0 then
		  conn.execute("update orderdetail set BuyQty="&cdbl(qty)&",mark=4 where id="&orderdetailID&"")
		else
		  conn.execute("update orderdetail set qty=qty-"&cdbl(qty)&" where id="&orderdetailID&"")
		  '复制一条记录
	 conn.execute("INSERT INTO orderdetail([Order],[Uid],[usertype],[Sid],[Pid],[Title],[url],[weight],[StoragedTime],[Price],[transCost],[Qty],[BuyQty],[Content],[mark],[Issue],[Type],[Prepaid],[BidsPrice],[EndTime],[Addtime],[RefundAmount],[isPackage],[Location]) SELECT [Order],[Uid],[usertype],[Sid],[Pid],[Title],[url],[weight],[StoragedTime],[Price],[transCost],'"&qty&"','"&qty&"',[Content],4,[Issue],[Type],[Prepaid],[BidsPrice],[EndTime],[Addtime],[RefundAmount],[isPackage],[Location] FROM orderdetail where id="&orderdetailID&"")
		end if
		
		
	end if
	
	
	
	set rsu=server.createobject("adodb.recordset")
	sql="select * from PurchaseHistory"
	rsu.open sql,conn,1,3
	rsu.addnew
	rsu("title")=title
	rsu("sid")=sid
	rsu("url")=url
	rsu("price")=price
	rsu("qty")=qty
	rsu("TransferCost")=TransferCost
	rsu("TransportCost")=TransportCost
	rsu("totalcost")=totalcost
	rsu("paytype")=paytype
	rsu("addtime")=addtime
	rsu("pid")=pid
	rsu("Point")=Point
	RSU("orderdetailID")=orderdetailID
	rsu.update
	
	
	
	'更新
    'toqty=(cdbl(qty)+cdbl(buyqty))-cdbl(orderqty)
'	if toqty>=0 then
'	  conn.execute("update product set inventory=inventory+"&cdbl(toqty)&" where pid="&pid&"")
'	  conn.execute("update orderdetail set BuyQty="&cdbl(orderqty)&",mark=4 where id="&oid&"")
'	else
'	  conn.execute("update orderdetail set BuyQty="&cdbl(qty)&" where id="&oid&"")
'	end if


   
	
	response.write"<script language=JavaScript>{window.alert('添加成功！');window.location.href='buy.asp?id="&id&"&page="&request("page")&"';}</script>"
	rsu.close
	set rsu=nothing
	

end if





checkbox=request.Form("checkbox")
if checkbox<>"" then	
	if request.Form("button")="删除" then
		conn.execute("delete from PurchaseHistory where id in("&checkbox&")")
	end if
	response.Redirect "buy.asp?id="&request.QueryString("id")&"&page="&request.QueryString("page")&""
end if
%>